const router = require('koa-router')()
const userService = require('../controllers/mySqlControll.js')
const jwt = require('../untils/jwt.js')
const { getDetailedDateTime } = require('../untils/timeUtil.js')

// 获取商品表数据
router.get('/productList', async (ctx, next) => {
  try {
    const result = await userService.findProduct()
    if (!!result.length) {
      ctx.body = {
        msgType: '8000',
        code: 0,
        data: result,
        msg: '查询成功'
      }
    }
  } catch (error) {
    ctx.body = {
      msgType: '8002',
      code: 1,
      data: error,
      msg: '服务器异常'
    }
  }
})

// 获取商品中的注释
router.get('/productListName', async (ctx, next) => {
  try {
    const result = await userService.getProductAnnotations()
    if (!!result.length) {
      ctx.body = {
        msgType: '8000',
        code: 0,
        data: result,
        msg: '查询成功'
      }
    }

  } catch (error) {
    ctx.body = {
      msgType: '8002',
      code: 1,
      data: error,
      msg: '服务器异常'
    }
  }
})

// 获取商品中的类别
router.get('/getCategory', async (ctx, next) => {
  try {
    const result = await userService.getCategory()
    if (!!result.length) {
      ctx.body = {
        msgType: '8000',
        code: 0,
        data: result,
        msg: '查询成功'
      }
    }
  } catch (error) {
    ctx.body = {
      msgType: '8002',
      code: 1,
      data: error,
      msg: '服务器异常'
    }
  }
})


// 增加商品的数据
router.post('/addProduct', async (ctx, next) => {
  const { product_name, image_url, description, price, weight, inventory, category_id, created_at } = ctx.request.body
  console.log(product_name, description, price, weight, inventory, category_id, created_at);
  try {
    if (!product_name || !image_url || !description || !price || !weight || !category_id) {
      ctx.body = {
        msgType: '8002',
        code: 0,
        msg: '输入框不能为空'
      }
    } else {
      await userService.addProduct([product_name, image_url, description, price, weight, inventory, category_id, created_at]).then(res => {
        if (res.affectedRows !== 0) {
          ctx.body = {
            msgType: '8000',
            code: 0,
            msg: '添加成功'
          }
        }
      }
      )
    }

  } catch (error) {
    ctx.body = {
      msgType: '8002',
      code: 0,
      data: error,
      msg: '服务器异常'
    }
  }
})

// 修改商品的数据
router.post('/changeProduct', async (ctx, next) => {
  const { product_name, image_url, description, price, weight, inventory, category_id, updated_at, product_id } = ctx.request.body
  try {
    if (!product_name || !image_url || !description || !price || !weight || !category_id) {
      ctx.body = {
        msgType: '8002',
        code: 0,
        msg: '输入框不能为空'
      }
    }
    await userService.changeProduct(product_name, image_url, description, price, weight, inventory, category_id, updated_at, product_id).then(res => {
      if (res.affectedRows !== 0) {
        ctx.body = {
          msgType: '8000',
          code: 0,
          msg: '修改成功'
        }
      }
    }
    )
  } catch (error) {
    ctx.body = {
      msgType: '8002',
      code: 0,
      data: error,
      msg: '服务器异常'
    }
  }
})

// 删除商品的数据
router.post('/deleteProduct', async (ctx, next) => {
  const { product_id } = ctx.request.body
  console.log(product_id);
  try {
    const results = await Promise.all(product_id.map(async (id) => {
      console.log(id);
      return await userService.deleteProduct(id);
    }));
    // console.log(results);
    const affectedRows = results.reduce((total, result) => total + result.affectedRows, 0); // 获取所有删除操作受影响的行数总和
    console.log(affectedRows);
    if (affectedRows !== 0) {
      ctx.body = {
        msgType: '8000',
        code: 0,
        data: '',
        msg: '删除成功'
      }
    } else {
      ctx.body = {
        msgType: '8002',
        code: 0,
        data: '',
        msg: '删除失败'
      }
    }
  } catch (error) {
    ctx.body = {
      msgType: '8002',
      code: 0,
      data: error,
      msg: '服务器异常'
    }
  }
})

// 查询商品
router.post('/searchProduct', async (ctx, next) => {
  const { category_id, product_name } = ctx.request.body
  console.log(category_id, product_name);
  try {
    await userService.searchProduct(category_id, product_name).then(res => {
      if (!!res.length) {
        ctx.body = {
          msgType: '8000',
          code: 0,
          data: res,
          msg: '查找成功'
        }
      } else {
        ctx.body = {
          msgType: '8000',
          code: 0,
          data: [],
          msg: '查找成功'
        }
      }
    })
  } catch (error) {
    ctx.body = {
      msgType: '8002',
      code: 0,
      data: error,
      msg: '服务器异常'
    }
  }
})

// 获取类别的注释
router.get('/getCategoryAnnotations', async (ctx, next) => {
  try {
    const result = await userService.getCategoryAnnotations()
    if (!!result.length) {
      ctx.body = {
        msgType: '8000',
        code: 0,
        data: result,
        msg: '查询成功'
      }
    }

  } catch (error) {
    ctx.body = {
      msgType: '8002',
      code: 1,
      data: error,
      msg: '服务器异常'
    }
  }
})

// 增加类别
router.post('/addCategory', async (ctx, next) => {
  const { category_name, description, created_at } = ctx.request.body
  try {
    if (!category_name || !description) {
      ctx.body = {
        msgType: '8002',
        code: 0,
        msg: '输入框不能为空'
      }
    } else {
      await userService.addCategory([category_name, description, created_at]).then(res => {
        if (res.affectedRows !== 0) {
          ctx.body = {
            msgType: '8000',
            code: 0,
            msg: '添加成功'
          }
        }
      }
      )
    }

  } catch (error) {
    ctx.body = {
      msgType: '8002',
      code: 0,
      data: error,
      msg: '服务器异常'
    }
  }
})

// 修改类别
router.post('/changeCategory', async (ctx, next) => {
  const { category_name, description, category_id, updated_at } = ctx.request.body
  try {
    if (!category_name || !description) {
      ctx.body = {
        msgType: '8002',
        code: 0,
        msg: '输入框不能为空'
      }
    } else {
      await userService.changeCategory(category_name, description, updated_at, category_id).then(res => {
        if (res.affectedRows !== 0) {
          ctx.body = {
            msgType: '8000',
            code: 0,
            msg: '修改成功'
          }
        } else {
          ctx.body = {
            msgType: '8002',
            code: 0,
            msg: '修改失败'
          }
        }
      }
      )
    }

  } catch (error) {
    ctx.body = {
      msgType: '8002',
      code: 0,
      data: error,
      msg: '服务器异常'
    }
  }
})

// 删除类别
router.post('/deleteCategory', async (ctx, next) => {
  const { category_id } = ctx.request.body
  console.log(category_id);
  try {
    const results = await Promise.all(category_id.map(async (id) => {
      return await userService.deleteCategory(id);
    }));
    const affectedRows = results.reduce((total, result) => total + result.affectedRows, 0); // 获取所有删除操作受影响的行数总和
    console.log(affectedRows);
    if (affectedRows !== 0) {
      ctx.body = {
        msgType: '8000',
        code: 0,
        data: '',
        msg: '删除成功'
      }
    } else {
      ctx.body = {
        msgType: '8002',
        code: 0,
        data: '',
        msg: '删除失败'
      }
    }
  } catch (error) {
    ctx.body = {
      msgType: '8002',
      code: 0,
      data: error,
      msg: '服务器异常'
    }
  }
})

// 查询类别
router.post('/searchCategory', async (ctx, next) => {
  const { category_name } = ctx.request.body
  try {
    await userService.searchCategory(category_name).then(res => {
      if (!!res.length) {
        ctx.body = {
          msgType: '8000',
          code: 0,
          data: res,
          msg: '查找成功'
        }
      } else {
        ctx.body = {
          msgType: '8000',
          code: 0,
          data: [],
          msg: '查找成功'
        }
      }
    })

  } catch (error) {
    ctx.body = {
      msgType: '8002',
      code: 0,
      data: error,
      msg: '服务器异常'
    }
  }
})

// 获取上架商品表数据
router.get('/getShelving', async (ctx, next) => {
  try {
    const result = await userService.getShelving()
    if (!!result.length) {
      ctx.body = {
        msgType: '8000',
        code: 0,
        data: result,
        msg: '查询成功'
      }
    }
  } catch (error) {
    ctx.body = {
      msgType: '8002',
      code: 1,
      data: error,
      msg: '服务器异常'
    }
  }
})

// 获取推荐商品表数据
router.get('/getRecommended', async (ctx, next) => {
  try {
    const result = await userService.getRecommended()
    if (!!result.length) {
      ctx.body = {
        msgType: '8000',
        code: 0,
        data: result,
        msg: '查询成功'
      }
    }
  } catch (error) {
    ctx.body = {
      msgType: '8002',
      code: 1,
      data: error,
      msg: '服务器异常'
    }
  }
})

// 改变商品的上下架，推荐
router.post('/changeProductStatus', async (ctx, next) => {
  const { recommended,is_listed,product_id } = ctx.request.body
  // console.log(recommended, is_listed, product_id);
  try {
    await userService.changeProductStatus(product_id,is_listed,recommended  ).then(res => {
      if (res.affectedRows !== 0) {
        ctx.body = {
          msgType: '8000',
          code: 0,
          msg: '修改成功'
        }
      } else {
        ctx.body = {
          msgType: '8002',
          code: 0,
          msg: '修改失败'
        }
      }
    }
    )
  } catch (error) {
    ctx.body = {
      msgType: '8002',
      code: 0,
      data: error,
      msg: '服务器异常'
    }
  }
})

// 通过在类别中获得商品数据
router.get('/getProductDataInCategory', async (ctx, next) => {
  const { category_id } = ctx.request.query
  // console.log(category_id);
  // console.log(recommended, is_listed, product_id);
  try {
    await userService.getProductDataInCategory(category_id).then(res => {
      if (res.affectedRows !== 0) {
        ctx.body = {
          msgType: '8000',
          code: 0,
          data:res,
          msg: '修改成功'
        }
      } else {
        ctx.body = {
          msgType: '8002',
          code: 0,
          msg: '修改失败'
        }
      }
    }
    )
  } catch (error) {
    ctx.body = {
      msgType: '8002',
      code: 0,
      data: error,
      msg: '服务器异常'
    }
  }
})
// router.post('/getProductDataInCategory', async (ctx, next) => {
//   const { category_id } = ctx.request.body
//   console.log(category_id);
//   // console.log(recommended, is_listed, product_id);
//   try {
//     await userService.getProductDataInCategory(category_id).then(res => {
//       if (res.affectedRows !== 0) {
//         ctx.body = {
//           msgType: '8000',
//           code: 0,
//           data:res,
//           msg: '修改成功'
//         }
//       } else {
//         ctx.body = {
//           msgType: '8002',
//           code: 0,
//           msg: '修改失败'
//         }
//       }
//     }
//     )
//   } catch (error) {
//     ctx.body = {
//       msgType: '8002',
//       code: 0,
//       data: error,
//       msg: '服务器异常'
//     }
//   }
// })



// 通过product_id 查询具体商品数据
router.post('/getProductDataFromId', async (ctx, next) => {
  const { product_id } = ctx.request.body
  try {
    await userService.getProductDataFromId(product_id).then(res => {
      if (res.affectedRows !== 0) {
        ctx.body = {
          msgType: '8000',
          code: 0,
          data:res,
          msg: '查找成功'
        }
      } else {
        ctx.body = {
          msgType: '8002',
          code: 0,
          msg: '查找失败'
        }
      }
    }
    )
  } catch (error) {
    ctx.body = {
      msgType: '8002',
      code: 0,
      data: error,
      msg: '服务器异常'
    }
  }
})





module.exports = router